<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>审批管理</title>
		<link rel="stylesheet" href="/static/layui/css/layui.css" />
<link rel="stylesheet" href="/static/ui/css/ui.css" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">名称</label>
							<div class="layui-input-inline">
								<input type="text" name="name" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="approve-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md" lay-filter="approve-reset" lay-submit>
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="approve-table" lay-filter="approve-table"></table>
			</div>
		</div>

		
		<script type="text/html" id="approve-bar">
            {{# if(d.status === 0){ }}
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="pass">
				<i class="layui-icon layui-icon-edit"></i>通过
			</button>
            {{# if(d.val > 2){ }}
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="backprev">
				<i class="layui-icon layui-icon-delete"></i>打回上一步
			</button>
            {{# } }}
            <button class="pear-btn pear-btn-success pear-btn-sm" lay-event="backopen">
				<i class="layui-icon layui-icon-delete"></i>打回初始
			</button>
            {{# } }}
		</script>
		<script type="text/html" id="approve-time">
            {{# if(d.add_time != d.up_time){ }}
			{{layui.util.toDateString(d.up_time, 'yyyy-MM-dd HH:mm:ss')}}
            {{# } }}
		</script>
		<script type="text/html" id="approve-detail">
            <button class="pear-btn pear-btn-success pear-btn-sm" lay-event="viewdetail">
				<i class="layui-icon layui-icon-upload"></i>详情
			</button>
		</script>
		<script src="../config.js"></script>
		<script src="/static/layui/layui.js"></script>
		<script src="/static/ui/ui.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;
				let req = _req();

				let MODULE_PATH = "./";

				let cols = 
					[
						{
							title: 'ID',
							field: 'id',
							align: 'center',
							width:60
						},
						{
							title: '名称',
							field: 'approve_name',
							align: 'center'
						},
						{
							title: '状态',
							field: 'status_name',
							align: 'center',
							width:150
						},
						{
							title: '申请人',
							field: 'user_name',
							align: 'center',
							width:120
						},
                        {
							title: '审核人',
							field: 'op_name',
							align: 'center',
							width:120
						},
						{
							title: '详情',
							field: 'infoDetail',
							align: 'center',
							templet : '#approve-detail',
							width:120
						},
						{
							title: '添加时间',
							field: 'add_time',
							align: 'center',
							width:120
						},
						{
							title: '审核时间',
							field: 'up_time',
							align: 'center',
                            templet : '#approve-time',
							width:120
						}
					]
				if(!req.uid) {
					cols.push({
						title: '操作',
						toolbar: '#approve-bar',
						align: 'center',
						width: 350,
						fixed : 'right'
					})
				}

				table.render({
					elem: '#approve-table',
					url: apiUrl + 'approve/content?uid=' + req.uid + '&tid=' + req.tid + '&aid=' + req.aid,
					headers : getHeader(),
					parseData: function(res) {
						//console.log(res)
						return {
							"code": res.code, //解析接口状态
							"msg": res.message, //解析提示文本
							"count": res.data.count, //解析数据长度
							"data": res.data.list //解析数据列表
						};
					},
					page: true,
					cols: [cols],
					skin: 'line',

					//toolbar: '#approve-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

				table.on('tool(approve-table)', function(obj) {
					if (obj.event === 'pass') {
						window.pass(obj);
					} 
                    else if(obj.event === 'backopen') {
                        window.backopen(obj);
                    }else if(obj.event === 'backprev') {
                        window.backprev(obj);
                    }
					else if(obj.event === 'viewdetail') {
						window.viewdetail(obj);
					}
				});
				window.viewdetail = function(obj) {
					layer.open({
						type: 2,
						title: "查看" + obj.data.approve_name,
						shade: 0.1,
						offset: 'rt',
						area: ['90%', '100%'],
						anim: 1,
						maxmin: true,
						content: '/res/group/' + obj.data.infoDetail + '.html?id=' + obj.data.data_id
					});
				}
                window.pass = function(obj) {
                    layer.prompt({
						formType: 2,
						value: '',
						title: '通过审核-输入备注信息',
						area: ['300px', '50px'] //自定义文本域宽高
					}, function(value, index, elem) {
                        console.log(value)
                        if(!value || value == '') {
                            layer.msg('请输入备注信息');
                            return false;
                        }
                        let postData = {
                            id : obj.data.id,
                            remark : value
                        }
                        _post(layui, 'approve/passApprove', postData, res => {
                            console.log(res)
                            layer.close(index);
                            table.reload('approve-table');
                        })
                    })
                }
                window.backprev = function(obj) {
                    layer.prompt({
						formType: 2,
						value: '',
						title: '打回上一步-输入备注信息',
						area: ['300px', '50px'] //自定义文本域宽高
					}, function(value, index, elem) {
                        let postData = {
                            id : obj.data.id,
                            remark : value
                        }
                        _post(layui, 'approve/backPrevArrpove', postData, res => {
                            console.log(res)
                            layer.close(index);
                            table.reload('approve-table');
                        })
                    })
                }
                window.backopen = function(obj) {
                    layer.prompt({
						formType: 2,
						value: '',
						title: '打回初始-输入备注信息',
						area: ['300px', '50px'] //自定义文本域宽高
					}, function(value, index, elem) {
                        let postData = {
                            id : obj.data.id,
                            remark : value
                        }
                        _post(layui, 'approve/backOpenArrpove', postData, res => {
                            console.log(res)
                            layer.close(index);
                            table.reload('approve-table');
                        })
                    })
                }
				table.on('toolbar(approve-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					}
				});
				// 监听搜索操作

				form.on('submit(approve-query)', function(data) {
					table.reload('approve-table', {
						where: {
							param : $('form').serialize()
						},
						page: {
					        curr: 1
					    }
					})
					return false;
				});
				form.on('submit(approve-reset)', function(data){
					table.reload('approve-table', {
						where: {
							param : ''
						},
						page: {
					        curr: 1
					    }
					});
					return false;
				})

				window.add = function() {
					layer.open({
						type: 2,
						title: '新增',
						shade: 0.1,
						offset: 'rt',
						area: ['80%', '100%'],
						anim: 1,
						maxmin: true,
						content: MODULE_PATH + 'edit.html'
					});
				}

				
				window.remove = function(obj){
		            layer.confirm('确定要删除', {icon: 3, title:'提示'}, function(index){
		                layer.close(index);
		                _post(layui, 'approve/del',{id : obj.data.id}, res => {
							layer.msg('删除成功',{icon:1,time:1000},() => {
								obj.del();
							});
						}, err => {
							//layer.msg('删除失败',{icon:2,time:1000});
						})
		            });
		        }


				window.refresh = function(param) {
					table.reload('approve-table');
				}
			})
		</script>
	</body>
</html>